<?php
class IndexController extends Vnf_Controller_Action {
    protected $_dir;

    public function indexAction() {
        $this->setLayout('cms');
        $this->prepareData();

        $currentPage = $this->view->currentPage;
        $template = @$currentPage['template'];

//        echo "HHHHHHHHH--------";
        if ('' !=  $template && null != $template)
            $this->_helper->viewRenderer->renderScript($template);
            //exec()
//            $this->_helper->viewRenderer->renderScript('cms/list.phtml');
          //  return;
    }

    public function centerAction() {
        echo "<hr>This content output from centerAction";
        $this->_helper->viewRenderer->setViewScriptPathSpec('cms/detail.phtml');
    }
    public function headerAction() {
//        echo "kkk";
        $category = new Category();

//        echo $this->_helper->viewRenderer->getViewScriptPathSpec();die;
//        echo $this->_helper->viewRenderer->getViewScript();die;
//        die;
//        $this->_helper->viewRenderer->setViewScriptPathSpec(':controller/:action.:suffix');
//        echo $this->_helper->viewRenderer->getViewScriptPathSpec();die;
//        $root = $category->getRoot();
//        $data = $category->getTreeOfNode($root->category_id);
        $this->view->menu = $category->getMainMenu();

//        print_r($category->getMainMenu());

//        echo count($data);
        //print_r($data);
//        return;
    }

    public function footerAction() {
//        $category = new Category();

//        $root = $category->getRoot();
//        $data = $category->getTreeOfNode($root->category_id);
//        $data = $category->getCacheOfCategory();

//        print_r($category->getMainMenu());

//        echo count($data);
//        print_r($data);
//        return;
    }

    public function leftAction() {
        // set another template
        // $this->_helper->viewRenderer->setScriptAction('test');
        // :controller/:action.:suffix

        // render to another content
        //$this->_helper->viewRenderer->setViewScriptPathSpec('cms/detail.phtml');


        $currentChild = $this->view->currentChild;

        if (count($currentChild) > 0)
            $this->view->leftMenuID = $this->view->currentPageID;
        else {
            $currentPage = $this->view->currentPage;
//            print_r($currentPage);
            $this->view->leftMenuID = $currentPage['parent'];
        }
    }

    public function rightAction() {
    }

    public function blockcmsAction() {
        @$this->view->blockParams = $params = $this->_getAllParams();
        @$this->view->module = $params['module'];
        @$this->view->controller = $params['controller'];
        @$this->view->action = $params['action'];
        @$this->view->name = $name = $params['id'];
        @$this->view->showContent = $showContent = $params['showContent'];
        @$this->view->content = $showContent = $params['content'];
        @$this->view->title = $showContent = $params['title'];

        //
        $isBlockNameAldreadyExists = false;
        // get all block has been register

        $manageBlockCms = Vnf::getBlockCms();

        // check id of block has exists in this page
        if (!array_key_exists($name, $manageBlockCms)) {
            $manageBlockCms[$name] = array('id' => $name);
            Zend_Registry::set('blockcms', $manageBlockCms);
        } else {
            $isBlockNameAldreadyExists = true;
            echo "<br>$name already exists<br>";
        }
        $this->view->isBlockNameAldreadyExists = $isBlockNameAldreadyExists;
    }

    public function endcmsAction() {
        Zend_Registry::set('blockcms', array());
    }

    public function translateAction() {
        $translate = new Zend_Translate('tmx', PATH_LANG_MODULE . 'items/english.tmx', 'en');
//        echo $translate->
    }

    public function listThumbnailsAction() {
		$this->listAction();
	}
    public function listAction() {
        // get category id
        $catID = $this->_request->getParam('cid', 0);
        $currentPage = $this->view->currentPageID;

        // get child page
        $pageWithSubPage = $this->view->currentChild;
        // join current page
        $pageWithSubPage[] = $currentPage['category_id'];
        // parse to string
        $strPageWithSubPage = implode(',', $pageWithSubPage);

        if ($catID > 0) {
            $objItem = new Item();

            $where = "category_id in ($strPageWithSubPage)";
            $totalRecords = $objItem->getTotalRecords("$where");

            $currentPaging = (int) $this->_request->getParam('page', 1);
            $currentPaging--;
            $numberPageView = 5;
            $recordsPerPage = (int) $this->_request->getParam('records', 2);
            $totalPages = ceil($totalRecords/$recordsPerPage);
            $this->view->pages = $totalPages;

//            echo $currentPage;
            $link =  BASE_PATH . '/' . CURR_APP . "?pid=$currentPage";
            $tagID = "showbycat";
            $startRecord = $recordsPerPage * $currentPaging;
//            $order = 'CommentID DESC';
            $class = "paging";

            $paging = new Paging();
            $this->view->paging = $paging->getPaging($currentPaging + 1, $totalPages, $link, $numberPageView, $tagID, $class,'false');

            $pk = $objItem->getPrimaryKey();
            $items = $objItem->getRecords($where, "$pk DESC", $recordsPerPage, $recordsPerPage * $currentPaging);

            // get all items in current page and sub page
//            $items = $objItem->getRecords("$where", "item_id DESC");

            $this->view->allItems = $items;
        }
    }

    public function detailAction() {
        $this->setLayout('cms');
        $this->prepareData();

        $this->view->id = $this->_request->getParam('id', 0);
    }

    public function detailContentAction() {

        // get category id
        $itemID = $this->_request->getParam('id', 0);

        if ($itemID > 0) {
            $objItem = new Item();
            $item = $objItem->fetchRow("item_id = '$itemID'");

            $this->view->item = $item;
            $this->view->currentPageID = $item->category_id;
            $this->prepareData();
        }
    }

    public function prepareData() {
        $currentPageID = $this->_request->getParam('pid', $this->view->currentPageID);

        if ($currentPageID <= 0) {
            $category = new Category();

            $root = $category->getRoot();
            $currentPageID = $root->category_id;
        }
        $this->view->currentPageID =  $currentPageID;

        $category = new Category();

        $allCategory = $category->getCacheOfCategory();

        $this->view->allCategory = $allCategory;

        $currentPage = $allCategory[$currentPageID];
        $categoryPath = $currentPage['all_parent']!=""?$currentPage['all_parent'].",".$currentPageID : $currentPageID;

        $this->view->categoryPath = explode(',', $categoryPath);
        $this->view->currentPage = $currentPage;

//        print_r($currentPage);

        $childPage = array();
        @$childPage = $currentPage['children'];
        $this->view->currentChild = $childPage;
    }
}